<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>大类别修改</title>
		<link rel="stylesheet" href="../../static/assets/css/normalize.css">
		<link rel="stylesheet" href="../../static/assets/css/bootstrap.min.css">
		<link rel="stylesheet" href="../../static/assets/css/font-awesome.min.css">
		<link rel="stylesheet" href="../../static/assets/css/themify-icons.css">
		<link rel="stylesheet" href="../../static/assets/css/pe-icon-7-filled.css">
		<link rel="stylesheet" href="../../static/assets/css/flag-icon.min.css">
		<link href="../../static/assets/weather/css/weather-icons.css" rel="stylesheet" />
		<link href="../../static/assets/calendar/fullcalendar.css" rel="stylesheet" />
		<link href="../../static/assets/css/charts/chartist.min.css" rel="stylesheet">
		<link href="../../static/assets/css/lib/vector-map/jqvmap.min.css" rel="stylesheet">
		<link href="../../static/css/pagination.css" rel="stylesheet">
		<link rel="shortcut icon" href="../../static/images/favicon.ico">
		<style type="text/css">
			body{background: none;}
			label.error{color: red;display: block;padding-top: 3px;}
		</style>
	</head>
	<body>
		<div class="card">
			<div class="card-header"><strong>修改大类别</strong></div>
			<div class="card-body card-block">
				<form>
					<input type="hidden" name="pcId">
					<div class="row form-group">
						<div class="col col-md-2"><label class=" form-control-label">类别名称:</label></div>
						<div class="col-md-10 col-sm-10">
							<input type="text" class="form-control" name="pcName">
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-2"><label class=" form-control-label">所属类型:</label></div>
						<div class="col-md-10 col-sm-10">
							<input type="radio" name="pcType" value="0">商家
							<input type="radio" name="pcType" value="1">频道
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-2"><label class=" form-control-label">类别配图:</label></div>
						<div class="col-md-2 col-sm-10">
							<img id="image" width="130" height="150">
						</div>
						<input type="hidden" name="pcImg"/>
						<input type="file" name="img" onchange="showImg(this)" >
					</div>
					<div class="form-actions form-group">
						<button type="submit" class="btn btn-primary" id="but">保存</button>
						<button type="button" class="btn btn-default" onclick="goBack()">返回</button>
					</div>
				</form>
			</div>
		</div>	
	</body>
	<script src="../../static/assets/js/vendor/jquery-2.1.4.min.js"></script>
	<script src="../../static/jquery-validate/jquery.validate.min.js"></script>
	<script src="../../static/layer/layer.js"></script>
	<script src="../../static/js/ajax.js"></script>
	<script src="../../static/js/jquery-validate/additional-methods-remoteAjax.js"></script>
	<script type="text/javascript">
		let data;
		$(function(){
			//数据回显
			let id = decodeURI(location.href.split("=")[1]);
			ajaxHttp({
				url:"system/parCategory/getById",
				data:{id:id},
				success:function(msg){
					if(msg.code==1){
						data = msg.data;
						$("[name='pcId']").val(data.pcId);
						$("[name='pcName']").val(data.pcName);
						$("[name='pcType']")[data.pcType].checked = true;
						if (data.pcImg!=null){
							$("[name='pcImg']").val(data.pcImg);
							$("img").prop("src",data.pcImg);
						}
					}
				}
			})
		})
		
		
		function showImg(input){
		    $("#image").prop("src", window.URL.createObjectURL(input.files[0]));
		}
		
		$("form").validate({
			rules: {
				pcName: {
					required: true,
					maxlength: 50,
					remoteAjax: {
						type:"get",
						url:"system/parCategory/checkName",
						data:{pcId:function(){
							return $("[name='pcId']").val();
						}}
					}
				}
			},
			messages: {
				pcName: {
					required: "类别名称不能为空",
					maxlength:"类别名称超过50字符",
					remoteAjax:"类别名称重复"
				}
			},
			submitHandler: function(){
				editSave();
			}
		});
		
		function editSave(){
			layer.confirm("确定保存么",{icon:3,yes:function (index){
				//文件上传
				if($("[name='img']").val()!=""){
					saveImg()
				}
				saveData();
				layer.close(index);
			}})
		}
		
		//文件上传
		function saveImg(){
			let file = $("[name='img']")[0].files[0];
			let formData = new FormData();
			formData.append("file", file);
			fileHttp({
				url:"system/file/fileUpload",
				type:"post",
				dataType:"json",
				async:false,
				data:formData,
				success:function(msg){
					//保存数据
					$("[name='pcImg']").val(msg.data);
				}
			})
		}
		
		//数据保存
		function saveData(){
			ajaxHttp({
				url:"system/parCategory/editSave",
				type:"patch",
				data:form2JsonStrWithOutFile($("form")),
				success:function(msg){
					if(msg.code==1){
						location.href="parCatList.html";
					}else{
						layer.alert(msg.desc,{icon:5});
					}
				}
			})
		}
		
		function goBack(){
			location.href="parCatList.html";
		}
		
	</script>
</html>
